package com.ypj.indusynergyems.controller;

import com.ypj.indusynergyems.dao.entity.GoodsEntity;
import com.ypj.indusynergyems.dao.entity.GoodsTypeEntity;
import com.ypj.indusynergyems.dao.entity.ShopEntity;
import com.ypj.indusynergyems.dao.entity.UserEntity;
import com.ypj.indusynergyems.service.IShopService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequiredArgsConstructor
@Slf4j
public class ShopController {

    private final IShopService shopService;

    /**
     * 跳转注册
     */
    @GetMapping("/system/shop/register")
    public String toShopRegister(){
        return "/system/shop/register";
    }

    /**
     * 注册店铺
     */
    @PostMapping("/system/shop/register")
    public @ResponseBody String shopRegister(HttpSession session , ShopEntity shop){
        UserEntity user =(UserEntity) session.getAttribute("user");
        return shopService.shopRegister(user.getUserId(),shop);
    }

    //查询店铺列表并保存到session中
    /**
     * 根据用户查找本人管理店铺Id
     */
    @GetMapping("/system/shop/list/{userId}")
    public String shopList(@PathVariable("userId") int userId, Model model, HttpSession session){
        List<ShopEntity> shopList = shopService.findAllShopById(userId);
        session.setAttribute("shopList",shopList);
        return "/system/shop/list";
    }

    @GetMapping("/system/shop/list")
    public String shopList(HttpSession session){
        UserEntity user =(UserEntity) session.getAttribute("user");
        List<ShopEntity> shopList = shopService.findAllShopById(user.getUserId());
        session.setAttribute("shopList",shopList);
        return "/system/shop/list";
    }

    /**
     * 根据公司编码查找
     */

    /**
     * 根据商店用户Id查询商铺商品清单
     */
    @GetMapping("/system/shop/goods/{shopId}")
    public String shopGoods(@PathVariable("shopId") int shopId, Model model,HttpSession session){
        List<GoodsEntity> goodsList = shopService.findAllGoodsByShopId(shopId);
        //根据店铺Id查询店铺名称
        String shopName = shopService.findShopName(shopId);

        //根据商品类型Id查询商品类型
        for (GoodsEntity goodsEntity : goodsList){
            String name = shopService.getShopTypeName(goodsEntity);
            goodsEntity.setGoodsTypeName(name);
        }
        session.setAttribute("shopId",shopId);
        model.addAttribute("shopName",shopName);
        model.addAttribute("goodsList",goodsList);
        return "/system/shop/goods";
    }

    /**
     * 查看商品种类的列表
     */
    @GetMapping("/system/shop/goodsType/{shopId}")
    public String addGoodsType(@PathVariable("shopId") int shopId,Model model){
        List<GoodsTypeEntity> goodsTypeList = shopService.findAllShopType(shopId);
        String shopName = shopService.findShopName(shopId);
        model.addAttribute("shopName",shopName);
        model.addAttribute("goodsTypeList",goodsTypeList);
        return "system/shop/goodsTypeList";
    }

    /**
     * 跳转种类添加页面
     */
    @GetMapping("/system/shop/goodsTypeAdd")
    public String toAddGoodsType(){
        return "system/shop/goodsTypeAdd";
    }
    /**
     * 添加商品种类
     */
    @PostMapping("/system/shop/goodsType/add")
    public @ResponseBody String addGoodsType(GoodsTypeEntity goodsType){
        return shopService.addGoodsType(goodsType);
    }

    /**
     * 展示添加商品页面并显示添加种类列表
     */
//    @GetMapping("/system/shop/goodsAdd/{shopId}")
//    public String toAddGoods(@PathVariable("shopId") int shopId,Model model){
//        List<GoodsTypeEntity> goodsTypeList = shopService.findAllShopType(shopId);
//        model.addAttribute("goodsTypeList",goodsTypeList);
//        return "system/shop/goodsAdd";
//    }

    /**
     * 添加商品
     */
//    @PostMapping("/system/shop/goods/add")
//    public @ResponseBody String addGoods(GoodsEntity goods){
//        return shopService.addGoods(goods);
//    }

    /**
     * 查看店铺管理员
     */
    @GetMapping("/system/shop/admin/{shopId}")
    public String shopAdmin(@PathVariable("shopId") int shopId,Model model){
        List<UserEntity> userList = shopService.findAllShopAdmin(shopId);
        model.addAttribute("userList",userList);
        return "system/shop/adminList";
    }


    //修改店铺
    //1.向前端显示数据（根据店铺ID现在数据库查找该店铺的所有信息）
    //返回到 /system/shop/shopEdit
    @GetMapping("/system/shop/edit/{shop_id}")
    public String showEditShop(Model model, @PathVariable( "shop_id") int shop_id) {
        ShopEntity shop = shopService.findShopDetail(shop_id);
        model.addAttribute("shop", shop);
        model.addAttribute("authorities", shop);
        return "system/shop/shopEdit";
    }
    //2.修改店铺 （根据传过来的Shop）
    @PutMapping("/edit")
    public @ResponseBody String editShop(ShopEntity shop){
        log.info("=====编辑菜单，方法入参，shop={}===",shop);
        return shopService.editShop(shop);
    }
    //删除
    @DeleteMapping("/system/shop/delete")
    public @ResponseBody String deleteShop(@RequestParam("id") int shopId){
        log.info("======删除菜单，方法入参，shopid={}===",shopId);
        return shopService.deleteShop(shopId);
    }



}
